<template>
  <div class="rbt-pay-page">
    <div class="container">
      <RbtBread>
        <RbtBreadItem to="/">首页</RbtBreadItem>
        <RbtBreadItem to="/cart">购物车</RbtBreadItem>
        <RbtBreadItem>支付订单</RbtBreadItem>
      </RbtBread>
      <!-- 付款信息 -->
      <div v-if="order" class="pay-info">
        <span class="icon iconfont icon-querrn2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p v-if="order.countdown > -1">
            支付还剩 <span>{{ timeText }}</span
            >,超时后将取消订单
          </p>
          <p v-else>订单已经超时</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ order.payMoney }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a href="javascript:;" class="btn wx"></a>
          <a :href="payUrl" target="_blank" class="btn alipay"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a href="javascript:;" class="btn">招商银行</a>
          <a href="javascript:;" class="btn">工商银行</a>
          <a href="javascript:;" class="btn">建设银行</a>
          <a href="javascript:;" class="btn">农业银行</a>
          <a href="javascript:;" class="btn">交通银行</a>
        </div>
      </div>
    </div>
  </div>
  <RbtDialog v-model:visible="visibleDialog" title="正在支付...">
    <div class="pay-wait">
      <img src="@/assets/images/loading.gif" alt="" />
      <div v-if="order">
        <p>如果支付成功：</p>
        <RouterLink :to="`/member/order/${order.id}`"></RouterLink>
        <p>如果支付失败：</p>
        <RouterLink to="/">查看相关疑问</RouterLink>
      </div>
    </div>
  </RbtDialog>
</template>

<script setup lang="ts">
import { findOrder } from '@/api/order'
import { useRoute } from 'vue-router'
import { useIntervalFn } from '@vueuse/core'
import dayjs from 'dayjs'

// 订单
const order = ref<any>()
// 路由信息
const route = useRoute()
// 支付提示
const visibleDialog = ref(false)
/**
 * 支付倒计时函数
 */
const usePayTime = () => {
  const time = ref(0)
  const timeText = ref('')
  // 倒计时逻辑
  const { pause, resume } = useIntervalFn(
    () => {
      time.value--
      timeText.value = dayjs.unix(time.value).format('mm分ss秒')
      if (time.value <= 0) {
        pause()
      }
    },
    1000,
    false
  )
  onUnmounted(() => {
    pause()
  })
  // 开启定时器
  const start = (countdown: number) => {
    time.value = countdown
    timeText.value = dayjs.unix(time.value).format('mm分ss秒')
    resume()
  }
  return {
    start,
    timeText
  }
}
const { start, timeText } = usePayTime()
// 查询订单
findOrder(route.query.id as string).then(res => {
  // 设置订单
  order.value = res.data.result
  // 后端提供 countdown 倒计时秒数
  if (res.data.result.countdown > -1) {
    start(res.data.result.countdown)
  }
})

// 支付地址
const redirect = encodeURIComponent('http://www.corho.com:8080/#/pay/callback')
const payUrl = `http://pcapi-xiaotuxian-front-devtest.itheima.net/pay/aliPay?orderId=${route.query.id}&redirect=${redirect}`
</script>

<style scoped lang="scss">
.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;
  .icon {
    font-size: 80px;
    color: #1dc779;
  }
  .tip {
    padding-left: 10px;
    flex: 1;
    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }
      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }
      &:last-child {
        color: $rbtColor;
        font-size: 20px;
      }
    }
  }
}
.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;
  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;
    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }
  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666;
    display: inline-block;
    &.active,
    &:hover {
      border-color: $rbtColor;
    }
    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png)
        no-repeat center / contain;
    }
    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg)
        no-repeat center / contain;
    }
  }
}
.pay-wait {
  display: flex;
  justify-content: space-around;
  p {
    margin-top: 30px;
    font-size: 14px;
  }
  a {
    color: $rbtColor;
  }
}
</style>
